/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package woowcrawler;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import woowcrawler.html.*;

/**
 *
 * @author Deise
 */
public class ManagerArtigo {

    public String GetTituloArtigo(WoowDocument doc) {
        try
        {
            Tag[] tags = doc.executeQuery("#divmain>h1>strong");
            return tags[0].getContent();
        } catch (Exception ex)
        {
            return "Erro ao tentar pegar o título";
        }

    }
    ///Feito por Deise e Douglas Neves

    public  String GetAbstract(WoowDocument doc) {
        try {

            Tag raiz = doc.getHtmlElement();
            Tag[] elemento = raiz.getTagsByAttribute("style", "display:inline");

            String[] resumo = new String[elemento.length];
            for (int i = 0; i < elemento.length; i++) {

                resumo[i] = elemento[i].getContent();

            }

            return resumo[0];
        } catch (Exception ex) {
            return null;
        }
    }
    ///Feito por Douglas Brito e Wisley

    public ArrayList<String> GetAutores(WoowDocument doc) {

        try {

            Tag raiz = doc.getHtmlElement();
            Tag[] elemento = raiz.getTagsByAttribute("title", "Author Profile Page");
            ArrayList<String> autores = new ArrayList<String>();

            for (int i = 0; i < elemento.length; i++) {

                autores.add(elemento[i].getContent());
            }

            return autores;
        } catch (Exception ex) {
            return null;
        }

    }

    public String GetPages(WoowDocument doc) {
        try {
            //Tag raiz = doc.getHtmlElement();
            Tag page = Tag.FromString(doc.getContentCustom("(?<=<td>Pages</td>)<td>[^<>]*</td>"));
            //Tag page=Tag.FromString(raiz.getTagString(Pattern.compile("(?<=<td>Pages</td>)<td>[^<>]*</td>"), 0));
            return page.getContent();
        } catch (Exception ex) {
            return null;
        }
    }
    //Feito por Deise

    public ArrayList<String> GetTags(WoowDocument doc) {
        try {
            Tag[] tags = doc.executeQuery("#divtags>span");
            ArrayList<String> tagsDocumento = new ArrayList<String>();
            for (int i = 0; i < tags.length; i++) {
                tagsDocumento.add(tags[i].getContent());
            }
            tagsDocumento.remove(0);
            return tagsDocumento;

        } catch (Exception ex) {
            return null;

        }

    }
    
    ///Feito por Douglas Brito e Wisley
    public  String GetIsbn(WoowDocument doc) {
        try {

            return doc.getContentCustom("(?<=ISBN:\\s?)[0-9\\-]*");

        } catch (Exception ex) {
            return null;
        }
    }

    public  boolean SalvarPDF(String nome,WoowDocument doc) {
        try {

            Tag[] tag = doc.executeQuery("#divmain>[name=FullTextPdf]");
            String link = "";
            if (tag.length > 0) {
                //for (int i = 0; i < tag.length; i++) {

                    for (int j = 0; j < tag[0].getAttributes().length; j++) {
                        if (tag[0].getAttributes()[j].getName().equalsIgnoreCase("href")) {
                            link = tag[0].getAttributes()[j].getValue();
                        }
                    }
                    if (!link.equals("")) {
                        URL url = new URL("http://portal.acm.org/"+link);
                        InputStream in= url.openStream();

                        FileOutputStream saida = new FileOutputStream(nome+".pdf",false);
                        int ii = 0;
                        while ((ii = in.read()) >= 0) {
                            saida.write(ii);
                        }

                        saida.close();
                        in.close();

                    }
                //}
            }
            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public  String StopWord(String caminho, String texto) throws FileNotFoundException, IOException
    {
        File file = new File(caminho);
        String textoSeparado = texto.toUpperCase();
        if(file.exists())
        {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String linha;
            ArrayList<String> stopwords = new ArrayList<String>();
            while((linha = br.readLine()) != null )
            {
                stopwords.add(linha.toUpperCase());
            }
            br.close();
            for(String stop : stopwords)
            {
                textoSeparado = textoSeparado.replaceAll("\\b"+stop+"\\b", "");
            }
        }
        return textoSeparado;
    }
}
